Arduino in IPython

Import the Libraries


In [39]:
from IPython.display import clear_output
from time import sleep
import serial
import os
import sys

LED on/off


In [41]:
import serial
ser = serial.Serial('/dev/tty.usbmodem1411', 9600) # Establish the connection on a specific port
ser.write('on')


Out[41]:
2

In [42]:
import serial
ser = serial.Serial('/dev/tty.usbmodem1411', 9600) # Establish the connection on a specific port
ser.write('off')


Out[42]:
3

Blinking


In [43]:
import time, sys
import serial
ser = serial.Serial('/dev/tty.usbmodem1411', 57600) # Establish the connection on a specific port

for i in range(100):
    ser.write('on')
    time.sleep(0.4) #24Hz
    ser.write('off')
    time.sleep(0.4)
time.sleep(0.014) seems to be a lower limit

Works with:

/* Simple LED sketch */ int led = 13; // Pin 13 void setup() { pinMode(led, OUTPUT); // Set pin 13 as digital out // Start up serial connection Serial.begin(57600); // baud rate Serial.flush(); } void loop() { String input = ""; // Read any serial input while (Serial.available() > 0) { input += (char) Serial.read(); // Read in one char at a time delay(1); // Delay for 5 ms so the next char has time to be received } if (input == "on") { digitalWrite(led, HIGH); // on } else if (input == "off") { digitalWrite(led, LOW); // off } }